package org.ribizli.games.amoeba;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class RegisterDialog extends Dialog implements OnCancelListener, Button.OnClickListener {
  
  private EditText mUsernameET;
  private EditText mPasswordET;
  private EditText mPassword2ET;
  private Button mLoginBT;
  private GameActivity mGameActivity;

  public RegisterDialog(Context context) {
    super(context);
    mGameActivity = (GameActivity)context;
    setCancelable(true);
    setOnCancelListener(this);
    setTitle(R.string.registerDialogTitle);
    setContentView(R.layout.register);
    mUsernameET = (EditText) findViewById(R.id.register_username);
    mPasswordET = (EditText) findViewById(R.id.register_password);
    mPassword2ET = (EditText) findViewById(R.id.register_password2);
    mLoginBT = (Button) findViewById(R.id.register_button);
    mLoginBT.setOnClickListener(this);
  }

  @Override
  public void onCancel(DialogInterface dialog) {
    // TODO Auto-generated method stub
    
  }

  @Override
  public void onClick(View v) {
    String u = mUsernameET.getText().toString();
    String p = mPasswordET.getText().toString();
    String p2 = mPassword2ET.getText().toString();
    if ("".equals(u) || "".equals(p) || "".equals(p2)) {
      Toast.makeText(getContext(), R.string.errorMustNotEmpty, Toast.LENGTH_LONG).show();
    } else if (!p.equals(p2)) {
      Toast.makeText(getContext(), R.string.errorPasswordMismatch, Toast.LENGTH_LONG).show();
    } else {
      mPasswordET.setText("");
      mPassword2ET.setText("");
      this.dismiss();
      mGameActivity.register(u, p, null);
    }
  }
}
